package com.ssm.zyn.action;

import com.ssm.zyn.eo.ClassesEOpage;
import com.ssm.zyn.model.Classes;
import com.ssm.zyn.service.Classesservice;
import com.ssm.zyn.tool.ResponseMessage;
import com.ssm.zyn.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags="classes")
@RestController
@CrossOrigin
public class ClassesController {
    @Autowired
    private Classesservice classesservice;
    @ApiOperation(value = "班级添加方法")
    @RequestMapping(value="/addClasses",method = RequestMethod.GET)
    public ResponseMessage addClasses(Classes classes){
        int IsAdd=0;
        //传入数据的判断
        //（1）实体对象不能为空
        if(classes!=null){
            //(2)角色名称不能为空
            if(classes.getCname()!=null){
                //(3)添加角色数据
                IsAdd=classesservice.AddClasses(classes);
                if(IsAdd==1){
                    return Result.success("保存成功");
                }else if(IsAdd==-1){
                    return Result.error("班级重复不允许添加");
                }else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }
    @ApiOperation(value = "班级修改方法")
    @RequestMapping(value="/updateClasses",method = RequestMethod.GET)
    public ResponseMessage updateClasses(Classes classes){
        int isUpdate=0;
        //传入的参数不能为空
        if(classes!=null){
            //参数当中主键不能为0

            if (classes.getClassid()!=0){
                isUpdate=classesservice.UpdateClasses(classes);
                if(isUpdate==1){

                    return Result.success("更新成功");
                }else {
                    return Result.error("班级名重复，更新失败");
                }
            }
        }
        return  Result.error("传入参数不正确");
    }
    @ApiOperation(value = "班级删除方法")
    @RequestMapping(value="/delClasses",method = RequestMethod.GET)
    public ResponseMessage deClasses(int classid){
        if (classid!=0){
            int delInt=0;
            delInt=classesservice.DeleteClasses(classid);
            if(delInt>0){
                return Result.success("删除成功");
            }else {
                return Result.error("与中间表有关联，删除失败");
            }
        }else {
            return Result.error("删除的主键不能为0,请重新输入");
        }
    }
    @ApiOperation(value = "根据条件获取班级信息")
    @RequestMapping(value = "/getClassesAll",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public ResponseMessage getClassesAll(/*@RequestBody*/ ClassesEOpage classesEOpage){
        Map<String, List> map=new HashMap<>();
        List<ClassesEOpage> listClasses=classesservice.getWhereAll(classesEOpage);
        map.put("list",listClasses);
        return Result.success("200",map);
    }
}
